Skip to main content

BIP39 词表的验证器

项目描述

文档 Github Actions 构建状态 版本 Bitcointalk线程

bip39validator 示例运行

BIP39 Validator 是一个小程序,用于检查拉丁语言的 BIP39 词表。它支持检查词表的语义错误并实现三种不同的测试:

  • 最小 Levenshtein 距离测试

  • 最小唯一前缀长度

  • 最大长度测试

它还有一个 Python API,用于以编程方式运行每个测试并以交互方式探索结果。

<nav class="contents local" id="contents" role="doc-toc">

内容

</nav>

描述

BIP39 验证器检查单词表是否使用了 BIP39 标准中编写的最佳实践。这些检查是维护人员在合并单词表之前经常要求提交者遵守的检查。通过使用此工具,您无需手动验证列表的技术规则。

请注意,不支持使用诸如“单词听起来不能太相似”或“单词列表不能包含来自任何其他语言单词列表的单词”之类的规则进行验证。也不支持非拉丁语言的词表,例如阿拉伯语、希伯来语或 CJK 语言。

安装

您可以从 PyPI 或直接从 Github 上的源代码安装 BIP39 Validator。

要从 PyPI 安装:

pip3 install bip39validator

或者,要从源代码安装 BIP39 验证器,请转到发布页面,然后下载要安装的版本。解压包,进入新创建的目录,然后运行:

python3 setup.py install

跑步

您可以像这样调用 BIP39 验证器:

bip39validator [OPTIONS] {INPUT_FILE | URL_OF_TEXT_FILE}

一个,并且只有一个 INPUT_FILE 和 URL_OF_TEXT_FILE 应该被指定,其中 INPUT_FILE 是本地文件系统中的一个文件,而 URL_OF_TEXT_FILE 是一个 HTTP 或 HTTPS URL,指向具有 text/plain 的 mimetype 的 wordlist 文件。在这两种情况下,输入都必须是纯文本文件。

BIP39 验证器在验证过程中显示格式丰富的状态消息,但是也可以使用最少的诊断消息运行 BIP39 验证器,或将状态消息记录到文件中。命令行参数的完整列表如下:

命令行选项

选项

描述

-d, –min-levenshtein-距离

设置单词之间所需的最小 Levenshtein 距离(默认值:2)

-u, –min-initial-unique

设置单词之间所需的最小唯一初始字符(默认值:4)

-l,--最大长度

设置每个单词的最大长度(默认值:8)

-D, –no-levenshtein-distance

不要运行 Levenshtein 距离测试

-U, –no-initial-unique

不要运行唯一的初始字符测试

-L, –无最大长度

不运行最大长度测试

-o <文件>,--输出文件 <文件>

将所有控制台输出记录到附加文件

-a, –ascii

关闭控制台输出的富文本格式和进度条

-q,-安静

不显示测试失败的详细信息,只显示它们是成功还是失败

–nosane

禁止单词列表完整性检查。这可能会导致其他测试失败。

-v,--版本

打印版本号并退出

BIP39 验证器显示哪些验证测试成功以及成功的测试总数。

使用 API

BIP39 Validator 带有一个强大的 API 用于查询验证测试的结果。提供的最基本的类是BIP39WordList。它负责从文件、字符串缓冲区甚至 URL 创建单词列表对象。BIP39WordList对象是不可变的 ,不能从它们加载的对象中更改、添加或删除单词。要更改单词表,您需要在文件中更改它,然后再次从中创建BIP39WordList。

当测试失败时,它会引发ValidationFailed异常。这包含一个名为 status_obj的成员,该成员包含一个类,其中包含有关引发异常的测试的诊断信息。如果成功,验证测试也会返回此对象,但是有两种不同的方法来捕获测试状态的原因是因为用户最常见的是仅在测试失败时查看状态。

API 示例

以下是 BIP39 Validator API 的一些预期用途。

  • 验证 Levenshtein 距离 >= 2,然后找到所有 Levenshtein 距离小于 2 的词对:

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_lev_distance(2)
  # At this point, no word pairs have Levenshtein distance < 2.
except ValidationFailed as e:
  dists = e.status_obj.getwordpairs_lt(2)
  for wordpair in dists:
    word1 = wordpair[0]
    word2 = wordpair[1]
    # Do something with word1 and word2...
except InvalidWordList as e:
  print("Wordlist file is not well-formed")
  • 验证 Levenshtein 距离 >= 2,然后计算 Levenshtein 距离小于 2 的单词对的数量和百分比(假设 2048 个单词列表):

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_lev_distance(2)
  # At this point, the percentage and number of
  # words fulfilling the condition are 0.
except ValidationFailed as e:
  dists = e.status_obj.getwordpairs_lt(2)
  n = len(dists)
  prct = n/(2048*2048)
except InvalidWordList as e:
  print("Wordlist file is not well-formed")
  • 验证单词在至少 4 个初始字符中是唯一的,然后找到所有以“str”开头的单词(前缀 3 组“str”):

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_initial_chars(4)
  # At this point, all words are unique in at least 4 initial characters
except ValidationFailed as e:
  words = e.status_obj.similar_wordgroup("str")
  for word in words:
    # Do something with word...
except InvalidWordList as e:
  print("Wordlist file is not well-formed")
  • 验证单词在至少 4 个初始字符中是唯一的,然后计算其中至少包含两个单词的单词 prefix-4 组的数量和百分比:

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_initial_chars(4)
  # At this point, the percentage and number of
  # words fulfilling the condition are 0.
except ValidationFailed as e:
  groups = e.status_obj.similar_wordgroup_all(4)
  n = sum([c for c in groups.values() if len(c) >= 2])
  denom = len(groups.values())
  perc = n/denom
except InvalidWordList as e:
  print("Wordlist file is not well-formed")
  • 验证单词不超过 8 个字符,然后找出所有超过 8 个字符的单词:

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_max_length(8)
  # At this point, all words are no longer than 8 characters
except ValidationFailed as e:
  words = e.status_obj.getwords_gt(8)
  lines = e.status_obj.getlines_gt(8)
  for word, line in [*zip(words, lines)]:
    # Do something with word and line...
except InvalidWordList as e:
  print("Wordlist file is not well-formed")
  • 验证单词不超过 8 个字符,然后计算超过 8 个字符的单词的数量和百分比:

from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed

f = open('wordlist-en.txt')
try:
  wordlist = BIP39Wordlist('English wordlist', handle=f)
  wordlist.test_max_length(8)
  # At this point, the percentage and number of
  # words fulfilling the condition are 0.
except ValidationFailed as e:
  words = e.status_obj.getwords_gt(8)
  n = sum([w for w in words if len(w) > 8])
  perc = n/len(words)
except InvalidWordList as e:
  print("Wordlist file is not well-formed")

地方发展

首先,克隆这个仓库的master分支,然后新建一个 virtualenv:

python3 -m venv env-bip39validator
source env-bip39validator/bin/activate

然后使用以下命令安装模块依赖项:

pip3 install -r requirements.txt -r dev-requirements.txt

贡献

有关如何向该项目贡献问题和拉取请求的详细信息,请参阅 CONTRIBUTING.md。

执照

BIP39 验证器是根据 MIT 许可证提供的,可以在LICENSE 文件中找到。通过使用、分发或参与本项目,您同意本许可的条款和条件。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

bip39validator-1.0.7.ta​​r.gz (26.9 kB 查看哈希)

已上传 source

内置分布

bip39validator-1.0.7-py3-none-any.whl (33.4 kB 查看哈希)

已上传 py3